package com.leyu.mall.common.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoginFilter implements Filter
{
	private static Logger		log	= LoggerFactory.getLogger(LoginFilter.class);
	private ServletContext	sc;

	public LoginFilter()
	{
	}

	public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain chain) throws IOException, ServletException
	{
		HttpServletRequest request = (HttpServletRequest) srequest;
		HttpServletResponse response = (HttpServletResponse) sresponse;
		// String backurl = request.getServletPath();
		String redirectUrl = "login";
		HttpSession session = request.getSession(true);
		Object loginInfoObj = session.getAttribute("YW-USER-SESSION");
		if (loginInfoObj == null)
		{
			log.error("login error...");
			response.sendRedirect(redirectUrl);
		} else
		{
			log.debug("login..." + loginInfoObj);
		}
		chain.doFilter(request, response);
	}

	public void init(FilterConfig config) throws ServletException
	{
		sc = config.getServletContext();
	}

	public void destroy()
	{
	}
}
